package com.maple.utils.util

import android.app.Activity
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.EditText
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.blankj.utilcode.util.*
import com.blankj.utilcode.util.StringUtils
import com.google.gson.reflect.TypeToken
import java.io.Serializable

class UIUtils {

    companion object {

        fun getString(@StringRes resId: Int): String {
            return StringUtils.getString(resId)
        }

        fun getColor(@ColorRes resId: Int): Int {
            return ColorUtils.getColor(resId)
        }

        fun getDrawable(@DrawableRes resId: Int): Drawable {
            return ResourceUtils.getDrawable(resId)
        }

        fun getSize(dp: Float): Int{
            return SizeUtils.dp2px(dp)
        }

        @JvmStatic
        fun getValue(v: String?,def: String? = ""): String{
            if(TextUtils.isEmpty(v) || TextUtils.equals("null",v)) return def?:""
            return v!!
        }

        fun editTransformation(field: EditText, iv: ImageView, showIcon: Drawable, hideIcon: Drawable) {
            if(field.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
                field.transformationMethod = PasswordTransformationMethod.getInstance()
                iv.setImageDrawable(hideIcon)
            } else {
                field.transformationMethod = HideReturnsTransformationMethod.getInstance()
                iv.setImageDrawable(showIcon)
            }
            field.setSelection(field.text.length)
        }

        fun hideKeyboard(context: Activity) {
            if(KeyboardUtils.isSoftInputVisible(context)) {
                KeyboardUtils.hideSoftInput(context)
            }
        }

        private var lastClickTime: Long = 0L
        private const val DELAY_TIME: Long = 600L

        fun isFastClick(delay: Long = DELAY_TIME): Boolean {
            val time = System.currentTimeMillis()
            val lastTime = time - lastClickTime
            if (0L < lastTime && lastTime < delay) {
                return true
            }
            lastClickTime = time
            return false
        }

        inline fun <reified T> genericType() = object: TypeToken<T>() {}.type


        inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key?:"", T::class.java)
            else -> @Suppress("DEPRECATION") getSerializable(key?:"") as? T
        }

        inline fun <reified T : Serializable> Intent.serializable(key: String?): T? = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializableExtra(key?:"", T::class.java)
            else -> @Suppress("DEPRECATION") getSerializableExtra(key?:"") as? T
        }

        inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelable(key?:"", T::class.java)
            else -> @Suppress("DEPRECATION") getParcelable(key?:"") as? T
        }

        inline fun <reified T : Parcelable> Intent.parcelable(key: String?): T? = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelableExtra(key?:"", T::class.java)
            else -> @Suppress("DEPRECATION") getParcelableExtra(key?:"") as? T
        }

        fun getHidePhone(phone: String?): String{
            if(!TextUtils.isEmpty(phone) && phone!!.length >= 11) {
                val s = phone.substring(0,3)
                val e = phone.substring(7,11)
                return "${s}****${e}"
            }
            return ""
        }


        fun getEwmwb(ewmwb: String?): String {
            if(TextUtils.isEmpty(ewmwb)) return ""
            try {
                val a = JsonUtils.getString(ewmwb,"ewmwb")
                if(!TextUtils.isEmpty(a)) {
                    return a
                }
                val b = JsonUtils.getString(ewmwb,"EWMWB")
                if(!TextUtils.isEmpty(b)) {
                    return b
                }
            } catch (_: Exception) { }
            return ewmwb!!
        }
    }


}